﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UcAsp.Net.Model;
namespace UcAsp.Net.MSSQLDAL
{
    public class DefinedCodeDAL : UcAsp.Net.IDAL.IDefinedCodeDAL
    {
        public bool Delete(UcAsp.Net.Model.DefinedCode code)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                DefinedCode model = entities.DefinedCodes.FirstOrDefault(o => o.ID == code.ID);
                entities.DefinedCodes.Remove(model);
                entities.SaveChanges();
                return true;
            }
        }
        public bool Insert(UcAsp.Net.Model.DefinedCode code)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                entities.DefinedCodes.Add(code);
                entities.SaveChanges();
                return true;
            }
        }
        public System.Collections.Generic.List<UcAsp.Net.Model.DefinedCode> Select()
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                List<DefinedCode> list = entities.DefinedCodes.ToList();
                return list;
            }
        }
        public System.Collections.Generic.List<UcAsp.Net.Model.DefinedCode> SelectGroupType(string typename)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                List<DefinedCode> list = entities.DefinedCodes.Where(o=>o.DefinedCodeType==typename).ToList();
                return list;
            }
        }
        public bool Update(UcAsp.Net.Model.DefinedCode code)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                System.Data.Entity.Infrastructure.DbEntityEntry<DefinedCode> entry = entities.Entry<DefinedCode>(code);
                entry.State = System.Data.EntityState.Modified;
                entities.SaveChanges();
                return true;
            }
        }
        public UcAsp.Net.Model.DefinedCode GetDefinedCodeName(int id)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.DefinedCodes.FirstOrDefault(o => o.ID == id);
            }
        }

        public List<UcAsp.Net.Model.DefinedCode> Select(int pagesize, int pageindex, out int pagecount)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                List<DefinedCode> list = new List<DefinedCode>();
                var query = entities.DefinedCodes;
                pagecount = (query.Count() / pagesize) + 1;
                list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
                return list;
            }
        }
    }
}
